package com.miaoji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miaoji.domain.dto.PageInfo;
import com.miaoji.domain.dto.Result;
import com.miaoji.domain.po.Cloth;
import com.miaoji.domain.po.OrderDetail;
import com.miaoji.domain.po.OrderInfo;
import com.miaoji.domain.vo.OrderVo;
import com.miaoji.mapper.OrderDetailMapper;
import com.miaoji.mapper.OrderInfoMapper;
import com.miaoji.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderInfoMapper orderInfoMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Override
    public Map<PageInfo,List<OrderVo>>  OrderSelectAll(int number,OrderInfo order) {
        if (order!=null){

        }
        QueryWrapper<OrderInfo> orderQuery = new QueryWrapper<>();
        if(order.getOrderNo()!=null && !order.getOrderNo().isEmpty()){
            orderQuery.eq("order_no",order.getOrderNo());
        }
        if(order.getOrderId()!=0){
            orderQuery.eq("order_no",order.getOrderNo());
        }
        if(order.getPickName()!=null){
            orderQuery.eq("pick_name",order.getPickName());
        }

        if(order.getOrderCreateTime()!=null ){
            orderQuery.eq("order_no",order.getOrderNo());
        }
        OrderVo orderVo = new OrderVo();
        List<OrderVo> orderVos = new ArrayList<>();
        Page<OrderInfo> page = new Page<>(number, 15);//number为页数,查询第number页
        Page<OrderInfo> orderInfoPage = orderInfoMapper.selectPage(page, orderQuery);
        List<OrderInfo> orderInfoList = orderInfoPage.getRecords(); // 获取当前页的数据列表;
        HashMap<PageInfo, List<OrderVo>> orderVosMap = new HashMap<>();
        PageInfo pageInfo = new PageInfo();
        long pages = orderInfoPage.getPages();//获取总页数
        pageInfo.setPageNumber(number);
        pageInfo.setPageSize(pages);
        List<Integer> orderIds = orderInfoList.stream().map(OrderInfo::getOrderId).collect(Collectors.toList());
        List<OrderDetail> orderDetailGroup = orderDetailMapper.selectByOrderIds(orderIds);
        for (OrderInfo orderInfo : orderInfoList) {
            orderVo.setOrderInfo(orderInfo);
            List<OrderDetail> orderDetails = orderDetailGroup.stream()
                    .filter(item -> item.getOrderId()==orderInfo.getOrderId())
                    .collect(Collectors.toList());
            orderVo.setOrderDetails(orderDetails);
            orderVos.add(orderVo);
        }
        orderVosMap.put(pageInfo,orderVos);
        return orderVosMap;
    }

    @Override
    public List<OrderDetail> OrderDetailSelect(int order_id) {
        List<OrderDetail> orderDetails = orderDetailMapper.orderDetailSelect(order_id);
        return orderDetails;
    }

    @Override
    public boolean OrderUpdateState(OrderInfo orderInfo) {
            int id=orderInfo.getId();
            int orderState=orderInfo.getOrderState();
            int result=orderInfoMapper.updateOrderState(id,orderState);
        if (result > 0) {
            return Result.ok("更新成功").getSuccess();
        } else {
            return Result.fail("更新失败").getSuccess();
        }
    }

    @Override
    public boolean OrderDeliverDetail(OrderInfo orderInfo) {
        //上传物流
        int result = orderInfoMapper.updateOrderDeliverDetail(orderInfo.getOrderId(), orderInfo.getLogisticsCompany(), orderInfo.getLogisticsInfomation());
        if (result>0){
            return Result.ok("更新成功").getSuccess();
        } else {
            return Result.fail("更新失败").getSuccess();
        }

    }

    @Override
    public boolean OrderDelete(OrderInfo orderInfo) {
        int id = orderInfo.getId();
        int result = orderDetailMapper.deleteOrderDetail(id);
        if (result>0){
            int orderInfoResult = orderInfoMapper.deleteOrderInfo(id);
            if (orderInfoResult>0){
                return Result.ok("更新成功").getSuccess();
            }else{
                orderDetailMapper.detailRollback(id);
                return Result.fail("更新失败").getSuccess();
            }
        } else {
            return Result.fail("更新失败").getSuccess();
        }
    }



    @Override
    public boolean OrderRefund(int order_id) {
        int result = orderInfoMapper.orderRefund(order_id);
        if (result>0){
            return Result.ok("修改成功").getSuccess();
        }else {
            return Result.fail("修改失败").getSuccess();
        }
    }
}
